﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.XSchEmA.Entity;

namespace Pixysoft.Framework.XSchEmA.Comparer
{
    class DataTypeComparer : IDataTypeCompareResult
    {
        private bool isSameTableName = true;
        private bool isSameColumnName = true;
        private bool isSameDataTypeName = true;
        private bool isSameLength = true;
        private bool isSamePrecision = true;
        private bool isSameScale = true;
        private bool isSameNullable = true;

        public IDataTypeCompareResult Compare(ColumnSchema aColumn, ColumnSchema bColumn)
        {
            if (aColumn == null || bColumn == null)
                return this;

            aColumn.Inspection();

            bColumn.Inspection();

            if (string.Equals(aColumn.ColumnName, bColumn.ColumnName, StringComparison.OrdinalIgnoreCase))
                isSameColumnName = true;
            else
                isSameColumnName = false;

            if (string.Equals(aColumn.TableName, bColumn.TableName, StringComparison.OrdinalIgnoreCase))
                isSameTableName = true;
            else
                isSameTableName = false;

            if (string.Equals(aColumn.DataType.DataType, bColumn.DataType.DataType, StringComparison.OrdinalIgnoreCase))
                isSameDataTypeName = true;
            else
                isSameDataTypeName = false;

            if (aColumn.DataType.Length == bColumn.DataType.Length)
                isSameLength = true;
            else
                isSameLength = false;

            if (aColumn.DataType.Precision == bColumn.DataType.Precision)
                isSamePrecision = true;
            else
                isSamePrecision = false;

            if (aColumn.DataType.Scale == bColumn.DataType.Scale)
                isSameScale = true;
            else
                isSameScale = false;

            if (aColumn.IsNullable == bColumn.IsNullable)
                isSameNullable = true;
            else
                isSameNullable = false;

            return this;
        }

        public bool IsSame
        {
            get
            {
                if (!isSameTableName)
                    return false;

                if (!isSameColumnName)
                    return false;

                if (!isSameDataTypeName)
                    return false;

                if (!isSameLength)
                    return false;

                if (!isSamePrecision)
                    return false;

                if (!isSameScale)
                    return false;

                if (!isSameNullable)
                    return false;

                return true;
            }
        }

        public bool IsSameTableName
        {
            get { return isSameTableName; }
            set { isSameTableName = value; }
        }
        public bool IsSameColumnName
        {
            get { return isSameColumnName; }
            set { isSameColumnName = value; }
        }
        public bool IsSameDataTypeName
        {
            get { return isSameDataTypeName; }
        }

        public bool IsSameLength
        {
            get { return isSameLength; }
        }
        public bool IsSamePrecision
        {
            get { return isSamePrecision; }
        }
        public bool IsSameScale
        {
            get { return isSameScale; }
        }
        public bool IsSameNullable
        {
            get { return isSameNullable; }
        }
        public bool IsSameDateType
        {
            get
            {
                return isSameDataTypeName && isSameLength && isSamePrecision && isSameScale;
            }
        }
        public bool IsSameColumn
        {
            get
            {
                return isSameTableName && IsSameColumnName && isSameDataTypeName && isSameLength && isSamePrecision && isSameScale && isSameNullable;
            }
        }
    }
}
